﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Qplus.protocol;

namespace Qplus.Infrastructure
{
    public class ConnectionManager
    {
        private static ConnectionManager instance;

        private TcpTransport connection;

        public static ConnectionManager getInstance()
        {
            if (instance == null)
            {
                instance = new ConnectionManager();
            }
            return instance;
        }


        /**
	    * 创建一个新的连接，并关闭上一个连接
	    * @param sddr
	    * @param port
	    * @param queue
	    * @return
	    * @throws IOException
	    */
	    public  TcpTransport createTcpConnection(string sddr, int port, PacketQueue queue) {
		    if(connection == null){
		    }else {
			    connection.halt();
		    }
		    connection = new TcpTransport(sddr, port, queue);
		    queue.setConnection(connection);
		    return connection;
	    }


        /**
	    * 发送数据包
	    * @param out
	    * @return
	    */
	    public Boolean send(OutPacket outpk){
		    if(connection != null){
                connection.send(outpk);
			    return true;
		    }else {
			    return false;
		    }
	    }

        public Boolean receive(InPacket inpk){
		    if(connection != null){
                connection.receive(inpk);
			    return true;
		    }else {
			    return false;
		    }
	    }


        /**
	     * 关闭连接
	     */
	    public  void closeConnection(){
		    if(connection == null){
		    }else {
			    connection.halt();
		    }
	}
	

    }

}
